package com.open.launcher.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


import com.open.launcher.activity.PlayerActivity;
import com.open.launcher.bean.details.IndexProgram;
import com.open.launcher.bean.details.PlayDetailsBean;
import com.open.launcher.callback.IRequestCallback;
import com.open.launcher.service.AHservice;
import com.open.launcher.utils.APPLog;
import com.open.launcher.utils.CacheDataPresenter;
import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;

import static android.content.ContentValues.TAG;


/**
 * Created by Shinelon on 2017/9/15.
 */

public class SitcomOrderPresenter {
    private boolean isDisplayed;
    private Callback callback;
    public List<List<IndexProgram>> listContent;
    private List<IndexProgram> indexPrograms;
    private int orderPosition;
    private PlayDetailsBean playDetailsBean;
    private Context mContext;

    public SitcomOrderPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    public void loadSitcom(int position) {
        Log.d("tag", "---------position--------" + position);
        if (listContent != null && listContent.size() > 0) {
            this.orderPosition = listContent.size() - position - 1;
            indexPrograms = listContent.get(orderPosition);
            Log.d("tag", "------indexPrograms-----" + indexPrograms.toString());
            if (indexPrograms != null && indexPrograms.size() > 0) {
                callback.onSitcomCompleted(this, indexPrograms.size());
            }
        }
    }

    public void toPlay(int position) {
        int index = orderPosition * 20 + position;
        Toast.makeText(mContext, "" + index, Toast.LENGTH_SHORT).show();
        if (playDetailsBean != null) {
            OkHttpUtils.post().url("http://"+ PreferencesUtils.getString(mContext, Consts.IP_ADDR_KEY)+ Paths.VOD_STATISTIC)
                    .addParams("id",playDetailsBean.getId()+"")
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.i(TAG, "onError: ");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.i(TAG, "onResponse: "+response);
                        }
                    });
            Intent intent = new Intent(mContext, PlayerActivity.class);
            intent.putExtra("data", playDetailsBean);
            intent.putExtra("type", 1000);
            intent.putExtra("position", index);
            mContext.startActivity(intent);
//            CacheDataPresenter.getPresenter(mContext).cacheSitcom(playDetailsBean.getId(), index);
        }
    }

    public void toCollection() {
//        CacheDataPresenter.getPresenter(mContext).cacheRecord(playDetailsBean, "collection_data", 20);
        Toast.makeText(mContext, "收藏成功！", Toast.LENGTH_SHORT).show();
    }

    public void toSitPlay(int position) {
        if (playDetailsBean != null) {
            Intent intent = new Intent(mContext, PlayerActivity.class);
            intent.putExtra("data", playDetailsBean);
            intent.putExtra("type", 1000);
            intent.putExtra("position", position);
            mContext.startActivity(intent);
        }
    }

    //加载数据
    public void loadItem(String url, final Callback callback) {
        this.callback = callback;
        AHservice service = new AHservice();
        service.sitcomOderCallback(url, new IRequestCallback() {
            @Override
            public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
                if (callback == null || !isDisplayed) {
                    APPLog.printInfo("onLoadComplete:callback :");
                    return;
                }
                playDetailsBean = (PlayDetailsBean) dto;
                if (playDetailsBean != null) {
                    getSelections(playDetailsBean.getEpisodes());
                    callback.setImg_view(playDetailsBean.getImage());
                    callback.setFilm_name(playDetailsBean.getName());
                    callback.setDirector_tv(playDetailsBean.getDirector());
                    callback.setIntroduce_tv(playDetailsBean.getInfo());
                    callback.setTostar_tv(playDetailsBean.getStaring());
                    callback.setYears_tv(playDetailsBean.getPublish_at());
                    callback.setCopyright(playDetailsBean.getCopyright());
                    if(playDetailsBean.getEpisodes()!=null&&playDetailsBean.getEpisodes().size()>0) {
                        callback.setAllCount("更新到" + playDetailsBean.getEpisodes().size() + "集"+"(总共"+playDetailsBean.getTotal_episode()+"集)");
                    }
                }
                if (listContent != null && listContent.size() > 0) {
                    callback.onMovieCompleted(SitcomOrderPresenter.this, listContent.size());
                    loadSitcom(listContent.size() - 1);
                }
            }

            @Override
            public void onLoading(String contentID) {

            }

            @Override
            public void onFailed(String code, String msg) {

            }
        });
    }

    private void getSelections(List<PlayDetailsBean.Episodes> episodesList) {
        if (episodesList != null && episodesList.size() > 0) {
            int count = episodesList.size();
            listContent = new ArrayList<>();
            if (count % 20 == 0) {
                for (int i = 0; i < count / 20; i++) {
                    List<IndexProgram> indexPrograms = new ArrayList<>();
                    for (int j = 0; j < 20; j++) {
                        int index = i * 2 * 10 + j;
                        IndexProgram indexProgram = new IndexProgram();
                        indexProgram.setId(episodesList.get(index).getId());
                        indexProgram.setName(episodesList.get(index).getName());
                        indexProgram.setLength(episodesList.get(index).getLength());
                        indexProgram.setUrl(episodesList.get(index).getUrl());
                        indexProgram.setInfo(episodesList.get(index).getInfo());
                        indexPrograms.add(indexProgram);
                    }
                    listContent.add(indexPrograms);
                }
            } else {
                int morethan = count % 20;
                int number = count - morethan;
                for (int i = 0; i < number / 20; i++) {
                    List<IndexProgram> indexPrograms = new ArrayList<>();
                    for (int j = 0; j < 20; j++) {
                        int index = i * 2 * 10 + j;
                        Log.d("tag", "------j---1---" + j);
                        IndexProgram indexProgram = new IndexProgram();
                        indexProgram.setId(episodesList.get(index).getId());
                        indexProgram.setName(episodesList.get(index).getName());
                        indexProgram.setLength(episodesList.get(index).getLength());
                        indexProgram.setUrl(episodesList.get(index).getUrl());
                        indexProgram.setInfo(episodesList.get(index).getInfo());
                        indexPrograms.add(indexProgram);
                    }
                    listContent.add(indexPrograms);
                }
                List<IndexProgram> indexPrograms = new ArrayList<>();
                for (int j = 0; j < morethan; j++) {
                    int index = number + j;
                    Log.d("tag", "------j---2---" + j);
                    IndexProgram indexProgram = new IndexProgram();
                    indexProgram.setId(episodesList.get(index).getId());
                    indexProgram.setName(episodesList.get(index).getName());
                    indexProgram.setLength(episodesList.get(index).getLength());
                    indexProgram.setUrl(episodesList.get(index).getUrl());
                    indexProgram.setInfo(episodesList.get(index).getInfo());
                    indexPrograms.add(indexProgram);
                }
                listContent.add(indexPrograms);
            }
            Log.d("tag", "--------listContent.size()---------" + listContent.toString());
        }
    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(SitcomOrderPresenter presenter, int count);

        void onSitcomCompleted(SitcomOrderPresenter presenter, int count);

        void setImg_view(String url);

        void setFilm_name(String text);

        void setDirector_tv(String text);

        void setTostar_tv(String text);

        void setYears_tv(String text);

        void setIntroduce_tv(String text);

        void setCopyright(String copyright);

        void setAllCount(String count);
    }

    public void onBindMainChanelView(MainViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        if (listContent != null && listContent.size() > 0) {
            int startIndex = 1;
            int endIndex = 20;
            int index = listContent.size() - position - 1;
            int count = listContent.get(index).size();
            startIndex = index * 20 + startIndex;
            endIndex = index * 20 + endIndex;
            if (count >= 20) {
                holder.setNumber(startIndex + " - " + endIndex);
            } else {
                holder.setNumber(startIndex + " - " + (startIndex + listContent.get(index).size() - 1));
            }

        }
    }

    public void onBindSitcomChanelView(MainViewSitcomHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        if (playDetailsBean != null) {
            List<String> sitcomArr=new ArrayList<>();
            List<PlayDetailsBean.Episodes> episodes = playDetailsBean.getEpisodes();
            for (int i=0;i<episodes.size();i++){
                sitcomArr.add(episodes.get(i).getName());
            }
//            ArrayList<String> list=new ArrayList<String>();

            String[] strings = new String[sitcomArr.size()];

            String[] strings1 = sitcomArr.toArray(strings);
            Log.i(TAG, "onBindSitcomChanelView: "+ Arrays.toString(strings1));
//            holder.setNumber(String.valueOf(orderPosition * 20 + position + 1), sitcomArr);
            holder.setNumber(listContent.get(orderPosition).get(position).getName(), strings1);
        }
    }

    public interface MainViewHolder {
        void setNumber(String index);
    }

    public interface MainViewSitcomHolder {
        void setNumber(String index, String[] sitcomArr);
    }

}
